package com.clown.hotItemsAnalysis

import java.util.Properties

import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}

object KafkaProducerUtil {
  def main(args: Array[String]): Unit = {
    writeToKafka("hotitems")

  }

  def writeToKafka(str: String): Unit = {
    val properties = new Properties()
    properties.setProperty("bootstrap.servers", "localhost:9092")
    properties.setProperty("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    properties.setProperty("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    val producer = new KafkaProducer[String, String](properties)
    // 从文件读取数据，逐行写入kafka
    val resource = getClass.getResource("/UserBehavior.csv")
    val bufferedSource = io.Source.fromFile(resource.getPath)
    for (line <- bufferedSource.getLines()) {
      val record = new ProducerRecord[String, String](str, line)
      producer.send(record)
    }
    producer.flush()
    producer.close()
  }


}
